package com.ujiuye.dao;
import com.ujiuye.pojo.studen;
import com.ujiuye.utils.pagetools;
import com.ujiuye.utils.utils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;
import java.util.List;

public class dao {//作业的dao
    utils utils =new utils();
    /**
     * @param search
     * @return
     */
    public int queryall(String search) {
        String sql = "select count(*) from student where sname like '%"+search+"%'";
        try {
            long q = (long)utils.qr.query(sql, new ScalarHandler());
            return (int)q;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    /**
     *分页查询
     * @param pt
     * @param search
     * @return
     */
    public List<studen> query(pagetools pt, String search) {
        String sql = "select * from student ";
        if(search!=null&&!"".equals(search)){
            sql+="where sname like '%" + search + "%'";
        }
        sql += "limit ?,?";
        try {
            return utils.qr.query(sql,new BeanListHandler<>(studen.class),pt.getIndex(),pt.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
